package cn.edu.scau.cmi.ema.service;

import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;


import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.Session;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;
import cn.edu.scau.cmi.ema.service.base.CmiEmaService;

import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.persistence.Query;
import java.util.Iterator;
@Service("Event9zwzService")
@Transactional

public class Event9zwzServiceImpl extends CmiEmaService implements Event9zwzService {
	public Event9zwzServiceImpl() {
	}

	@Transactional
	public Event9zwz saveEvent9zwz(Event9zwz event9zwz) {

		if (event9zwz.getId() == null) {

			Event9zwz existingEvent9zwz = event9zwzDAO.findEvent9zwzByPhysicalPrimaryKey(event9zwz.getNumber());

			if (isEquals(event9zwz, existingEvent9zwz)) {
				return existingEvent9zwz;
			}

			event9zwz = event9zwzDAO.store(event9zwz);
			event9zwzDAO.flush();

		} else {
			Event9zwz existingEvent9zwz = event9zwzDAO.findEvent9zwzByPrimaryKey(event9zwz.getId());

			event9zwz = event9zwzDAO.store(event9zwz);
			event9zwzDAO.flush();
		}

		return event9zwz;
	}

	public void deleteEvent9zwz(Event9zwz event9zwz) {
		event9zwzDAO.remove(event9zwz);
		event9zwzDAO.flush();
	}

	@Transactional
	public void deleteEvent9zwzs(Set<Integer> deleteEvent9zwzsId) {
		Query query = event9zwzDAO.createNamedQuery("deleteEvent9zwzsById", -1, -1);
		query.setParameter(1, deleteEvent9zwzsId);
		query.executeUpdate();
	}

	@Transactional
	public Set<Event9zwz> loadEvent9zwzs() {
		return event9zwzDAO.findAllEvent9zwzs();

	}

	@Transactional
	public Set<Event9zwz> loadReferenceEvent9zwzs(Set<Event9zwz> event9zwzs) {
		Iterator<Event9zwz> event9zwzsIterator = event9zwzs.iterator();
		Set<Event9zwz> referencedEvent9zwzs = new HashSet<>();
		while (event9zwzsIterator.hasNext()) {
			Event9zwz currentEvent9zwz = event9zwzsIterator.next();

		}
		return referencedEvent9zwzs;
	}

	public Set<Event9zwz> loadEvent9zwzsByIdSet(Set<Integer> event9zwzIdSet) {

		Iterator<Integer> event9zwzIdIterator = event9zwzIdSet.iterator();
		Set<Event9zwz> event9zwzSet = new HashSet<>();
		while (event9zwzIdIterator.hasNext()) {
			Integer id = event9zwzIdIterator.next();
			Event9zwz currentEvent9zwz = event9zwzDAO.findEvent9zwzById(id);
			event9zwzSet.add(currentEvent9zwz);
		}
		return event9zwzSet;
	}

	@Transactional
	public List<Event9zwz> findAllEvent9zwzs(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Event9zwz>(event9zwzDAO.findAllEvent9zwzs(startResult, maxRows));
	}

	@Transactional
	public Event9zwz findEvent9zwzByPrimaryKey(Integer id) {
		return event9zwzDAO.findEvent9zwzByPrimaryKey(id);
	}

	@Transactional
	public Integer countEvent9zwzs() {
		return ((Long) event9zwzDAO.createQuerySingleResult("select count(o) from Event9zwz o").getSingleResult())
				.intValue();
	}

	@Transactional
	public Integer countEvent9zwzs(CmiPagination page) {
		String sql = CmiSqlUtil.createCountHql(page, Event9zwz.class);
		return ((Long) event9zwzDAO.createQuerySingleResult(sql).getSingleResult()).intValue();
	}

	/**
	 * search for name with ajax
	 * 
	 */
	@Transactional
	public Integer countEvent9zwzsByName(String queryName) {
		Query query = event9zwzDAO.createNamedQuery("countEvent9zwzsByName", -1, -1);
		query.setParameter(1, queryName);
		return new Long((long) query.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countEvent9zwzsByDetachedCriteria(CmiPagination page) {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Event9zwz.class);
		Session session = entityManager.unwrap(Session.class);
		detachedCriteria.setProjection(Projections.rowCount());
		return new Long((long) detachedCriteria.getExecutableCriteria(session).uniqueResult()).intValue();
	}

	public Map<String, ?> loadEvent9zwzObjectReferencedSet(Event9zwz event9zwz) {
		HashMap<String, Object> objectReferencedSetMap = new HashMap<>();

		return objectReferencedSetMap;
	}

	public Map<String, ?> loadEvent9zwzClassReferencedSet(Event9zwz event9zwz) {
		HashMap<String, Object> classReferencedSetMap = new HashMap<>();

		return classReferencedSetMap;
	}

	public boolean isEquals(Event9zwz event9zwz, Object obj) {
		if (obj == event9zwz)
			return true;
		if (!(obj instanceof Event9zwz))
			return false;
		Event9zwz comparedEvent9zwz = (Event9zwz) obj;
		if (!CheckPropertyEqual(event9zwz.getNumber(), comparedEvent9zwz.getNumber())) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}
}

